package com.atmosphere.component.base;

import com.atmosphere.interfaces.IComponentKey;
import com.atmosphere.interfaces.component.IClocktimeComponent;
import com.atmosphere.util.TimeConverter;

/**
 * <code>ClocktimeComponent</code> defines the abstraction of a
 * component unit that is updated based on clock-time.
 *
 * @author Yi Wang (Neakor)
 * @version Creation date: 05-21-2009 19:37 EST
 * @version Modified date: 05-21-2009 19:44 EST
 */
public abstract class ClocktimeComponent extends AbstractComponent implements IClocktimeComponent {
	/**
	 * The <code>TimeConverter</code>.
	 */
	private final TimeConverter converter;
	/**
	 * The temporary <code>Float</code> update time.
	 */
	private float time;
	/**
	 * The <code>Float</code> change cycle length.
	 */
	private volatile float length;

	/**
	 * Constructor of <code>ClocktimeComponent</code>.
	 * @param key The <code>IComponentKey</code>.
	 */
	protected ClocktimeComponent(IComponentKey key) {
		super(key);
		this.converter = new TimeConverter();
	}
	
	@Override
	public void update(float interpolation) {
		// Get clock time.
		this.time += interpolation;
		if(this.time > this.length) this.time = 0;
		final float clocktime = this.converter.animToClock(this.time, false, this.length);
		// Update.
		this.update(clocktime, interpolation);
	}
	
	/**
	 * Update the component based on given clock-time and interpolation.
	 * @param clocktime The <code>Float</code> 24-hour, 100-minute clock-time.
	 * @param interpolation The <code>Float</code> time interpolation.
	 */
	protected abstract void update(float clocktime, float interpolation);

	@Override
	public void setCycleLength(float length) {
		this.length = length;
	}
	
	@Override
	public float getCycleLength() {
		return this.length;
	}
}
